
#include <ddk.h>
#include "CUffsDirStrEnumerator.h"
#define IncrementDllLockCount() do {} while(0)
#define DecrementDllLockCount() do {} while(0)

#include "_CUffsStrEnumerator.cpp"
#include <stdlib.h>
#include "uffserrors.h"

ECode CUffsStrEnumerator::Initialize(uffs_Object *f)
{
    URET ret;

    memcpy(&(m_findInfo.obj), f, sizeof(uffs_Object));

    ret = uffs_OpenFindObject(&m_findInfo, f->father, f->name);
    if (ret == U_FAIL) {
        kprintf("Can't open %s\n", f->name);
        return E_UFFS_PATH_ERROR;
    }

    m_bInit = TRUE;
    return NOERROR;
}

ECode CUffsStrEnumerator::UnInit()
{
    if (m_bInit) {
        m_findInfo.obj.pmode = 0;

        uffs_CloseFindObject(&m_findInfo);
        m_bInit = FALSE;
    }
    return NOERROR;
}

ECode CUffsStrEnumerator::Current(
    /* [out] */ WStringBuf *buffer)
{
    wchar_t wszName[MAX_FILENAME_LENGTH] = {0};

    if (buffer == NULL) {
        return E_INVALID_ARGUMENT;
    }

    if (m_szCurName[0] == '\0') {
        return E_INVALID_OPERATION;
    }

    MbsToWcs(wszName, m_szCurName, MAX_FILENAME_LENGTH);
    if (buffer == NULL || buffer->GetCapacity() < (int)wcslen(wszName)) {
        kprintf("buffer=%08x\n");
        return E_INVALID_ARGUMENT;
    }

    buffer->Copy(wszName);
    return NOERROR;
}

ECode CUffsStrEnumerator::MoveNext()
{
    URET ret;
    uffs_ObjectInfo info;
    m_szCurName[0] = '\0';

    ret = uffs_MoveNextObject(&info, &m_findInfo);
    if (info.info.name[0] == '\0' || ret == U_FAIL) {
        return E_FS_NO_SUCH_FILE;
    }

    strcpy(m_szCurName, info.info.name);
    return NOERROR;
}

ECode CUffsStrEnumerator::Reset()
{
    m_szCurName[0] = '\0';
    uffs_FindResetObject(&m_findInfo);
    return NOERROR;
}
